﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class EarolService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            txtCharge.Attributes["onkeyup"]="value=value.replace(/[^\\d]/g,'')";
            txtWzID.Attributes["onkeyup"] = "value=value.replace(/[^\\d]/g,'')";
            SqlConnection conn = DB.GetCon();
            conn.Open();
            this.AutoID(conn);
            this.bind();
            
        }
    }
    //根据数据库中的记录数自动产生一个数作为入库单ID
    public void AutoID(SqlConnection conn)
    {
        SqlCommand com = new SqlCommand("select count(*) from tb_Service", conn);
        int Count = Convert.ToInt32(com.ExecuteScalar());
        this.txtServiceID.Text = Convert.ToString(Count + 1);
        if (conn.State == ConnectionState.Open)
        {
            com.Dispose();
            conn.Close();
        }
    }

    public void bind()
    {
        DataSet ds1 = DB.reDs("select Title from tb_BigType where type='部门'");
        this.ddlDepartment.DataSource = ds1;
        ddlDepartment.DataTextField = "Title";
        ddlDepartment.DataBind();
        DataSet ds2 = DB.reDs("select * from tb_Enterprise  where type2='是'");
        this.ddlService.DataSource = ds2;
        ddlService.DataTextField = "Name";
        ddlService.DataBind();
    }
    //使页面恢复初始状态
    public void clear()
    {
        foreach (Control c in this.form1.Controls)//遍历控件查找文本框控件，并清空
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox t = (TextBox)c;
                t.Text = "";
            }
        }
        foreach (Control c in this.form1.Controls)//遍历控件查找DropDownList控件，并恢复默认选项
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
            {
                DropDownList d = (DropDownList)c;
                d.SelectedIndex = -1;
            }
        }
    }
    protected void txtWzID_TextChanged(object sender, EventArgs e)
    {
        SqlDataReader sdr1 = DB.reDr("select * from tb_Gdzc where GdzcID='" + this.txtWzID.Text.Trim() + "'");
        sdr1.Read();
        if (sdr1.HasRows)
        {

            SqlDataReader sdr = DB.reDr("select * from tb_GdzcDetails where GdzcID='" + this.txtWzID.Text.Trim() + "'");
            sdr.Read();
            this.txtName.Text = sdr["Name"].ToString();
            sdr.Close();
        }
        else
        {
            Response.Write("<script>alert('该固定资产不存在！')</script>");
        }
        sdr1.Close();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string ServiceID = this.txtServiceID.Text.Trim();
        string ServiceDate = this.txtServiceDate.Text.Trim();
        string SendMan = this.txtSendMan.Text.Trim();
        string WzID = this.txtWzID.Text.Trim();
        string Name = this.txtName.Text.Trim();
        string Department = this.ddlDepartment.SelectedValue.ToString();
        string Fault = this.txtFault.Text.Trim();
        string Sign = this.txtSign.Text.Trim();
        string Service = this.ddlService.Text.Trim();
        string Charge = this.txtCharge.Text.Trim();
        string Cause = this.txtCause.Text.Trim();
        string State = this.txtState.Text.Trim();
        try
        {
            DB.ExSql("insert into tb_Service values('" + ServiceID + "','" + ServiceDate + "','" + SendMan + "','" + WzID + "','" + Name + "','" + Department + "','" + Fault + "','" + Sign + "','" + Service + "'," + Charge + ",'" + Cause + "','" + State + "')");
            //使页面恢复初始状态
            this.clear();
            SqlConnection conn = DB.GetCon();
            conn.Open();
            AutoID(conn);//根据数据库中的记录数自动产生一个数作为入库单ID            
        }
        catch (SqlException oe)
        {
           Response.Write( oe.Message.ToString());
        }
    }
}
